#include "DataManager.h"
#include "cocos2d.h"
#include "CsvParser.h"

using namespace cocos2d;

#define SCORE_MAX "MaxSore"
#define CURRENT_TAG "current_tag"
#define CURRENT_LEVEL "current_lvl"
#define AD_NUM "ad_num"
#define MAX_LEVEL "max_lvl"
DataManager::DataManager(void)
{
	m_nMaxScore = CCUserDefault::sharedUserDefault()->getIntegerForKey(SCORE_MAX);

	m_nCurrentTag=CCUserDefault::sharedUserDefault()->getIntegerForKey(CURRENT_TAG);

	m_nMaxLevel = CCUserDefault::sharedUserDefault()->getIntegerForKey(MAX_LEVEL);

	initShop();
	initLevel();
	m_bMusic = true;
	m_nLevel=m_nMaxLevel;
	
}


DataManager::~DataManager(void)
{
}

int DataManager::getMaxSore()
{
	return m_nMaxScore;
}

int DataManager::updateScore( int nScore )
{
	if (nScore > m_nMaxScore)
	{
		m_nMaxScore = nScore;
		CCUserDefault::sharedUserDefault()->setIntegerForKey(SCORE_MAX,m_nMaxScore);
		CCUserDefault::sharedUserDefault()->flush();
	}

	return 0;
}

vector<stItem> * DataManager::getShopData()
{
	if (m_nCurrentTag == 0)
	{
		return &m_vCoco;
	}
	else
	{
		return &m_vFriut;
	}
}

bool DataManager::setTag( int nTag )
{
	m_nCurrentTag = nTag;
	CCUserDefault::sharedUserDefault()->setIntegerForKey(CURRENT_TAG, m_nCurrentTag);
	return true;
}

bool DataManager::getMusicStatus()
{
	return m_bMusic;
}

void DataManager::setMusicStatus( bool bMusic )
{
	m_bMusic = bMusic;
}

void DataManager::initShop()
{
	CsvParser* pParser = new CsvParser();
	pParser->openFile("1.csv");
	int nRow = pParser->getRow();
	int nCol = pParser->getCol();
	for (int i=0; i<nRow;i++)
	{
		if (strcmp("loco",pParser->getData(i,0))==0)
		{
			stItem item;
			item.strFile = pParser->getData(i,2);
			item.strName = pParser->getData(i,1);
			item.nScore = atoi(pParser->getData(i,3));
			m_vCoco.push_back(item);

		}

		if (strcmp("fruit",pParser->getData(i,0))==0)
		{
			stItem item;
			item.strFile = pParser->getData(i,2);
			item.strName = pParser->getData(i,1);
			item.nScore = atoi(pParser->getData(i,3));
			m_vFriut.push_back(item);
;
		}

	}
	delete pParser;
}

void DataManager::initLevel()
{
	CsvParser* pParser = new CsvParser();
	pParser->openFile("level.csv");
	int nRow = pParser->getRow();
	int nCol = pParser->getCol();
	for (int i=0; i<nRow;i++)
	{
		stLevel item;
		item.strName = pParser->getData(i,1);
		item.nScore = atoi(pParser->getData(i,2));
		item.nHit = atoi(pParser->getData(i, 3));
		if (item.nScore <=0)
		{
			continue;
		}
		CCLog("%s,%d,%d", item.strName.c_str(), item.nScore, item.nHit);
		m_vLevelData.push_back(item);

	}
	delete pParser;
}

int DataManager::getCurrentLevel()
{
	return m_nLevel;
}

int DataManager::updateLevel(int level)
{
	m_nLevel = level;
	if (m_nLevel > m_nMaxLevel)
	{
		CCUserDefault::sharedUserDefault()->setIntegerForKey(MAX_LEVEL, m_nLevel);
		m_nMaxLevel = m_nLevel;

		CCUserDefault::sharedUserDefault()->flush();
	}
	return 0;
}

int DataManager::getMaxLevel()
{
	return min(65, m_nMaxLevel);
}

int DataManager::updateMaxLevel(int nLevel)
{
	if (nLevel > m_nMaxLevel)
	{
		m_nMaxLevel = nLevel;
		CCUserDefault::sharedUserDefault()->setIntegerForKey(MAX_LEVEL, m_nLevel);
		CCUserDefault::sharedUserDefault()->flush();
	}
	return 0;
}
